/*
 * Copyright (C) 2014 Steven Kurilovitch
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.goodjaerb.swccg.ui;

import com.goodjaerb.swccg.core.filter.FilterField;
import java.awt.event.ActionListener;
import java.util.EnumMap;
import java.util.Set;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Steven Kurilovitch
 */
public final class ValidFieldFilterPanel extends javax.swing.JPanel {

    private EnumMap<FilterField, Set<String>> fields;
    
    /**
     * Creates new form ValidFieldCompareFilter
     */
    public ValidFieldFilterPanel() {
        super();
        initComponents();
        
        fieldDropdown.addActionListener((e) -> {
            comparatorDropdown.removeAllItems();
            switch(getSelectedFilterField().getCompareType()) {
                case CHARACTER:
                    comparatorDropdown.addItem(Comparator.IS);
                    comparatorDropdown.addItem(Comparator.IS_NOT);
                    break;
                case NUMERIC:
                    comparatorDropdown.addItem(Comparator.EQUALS);
                    comparatorDropdown.addItem(Comparator.LESS_THAN);
                    comparatorDropdown.addItem(Comparator.GREATER_THAN);
                    break;
                default:
                    break;
            }
            updateValues();
        });
        
        comparatorDropdown.addActionListener((e) -> {
           updateValues(); 
        });
    }
    
    public void addValueSelectedActionListener(ActionListener al) {
        valueDropdown.addActionListener(al);
    }
    
    private void updateValues() {
        FilterField ff = getSelectedFilterField();
        switch(getSelectedFilterField().getCompareType()) {
            case CHARACTER:
                valueDropdown.setModel(new DefaultComboBoxModel<>(fields.get(ff).toArray(new String[] { })));
                break;
            case NUMERIC:
                if(getSelectedComparator() == Comparator.EQUALS) {
                    valueDropdown.setModel(new DefaultComboBoxModel<>(fields.get(ff).toArray(new String[] { })));
                }
                else {
                    DefaultComboBoxModel<String> cbm = new DefaultComboBoxModel<>();
                    fields.get(ff).stream().forEach((value) -> {
                        try {
                            int v = Integer.parseInt(value);
                            cbm.addElement(value);
                        }
                        catch(NumberFormatException ex) {
                            // no problem.
                        }
                    });
                    valueDropdown.setModel(cbm);
                }
                break;
            default:
                valueDropdown.removeAllItems();
                break;
        }
    }
    
    public FilterField getSelectedFilterField() {
        return (FilterField)fieldDropdown.getSelectedItem();
    }
    
    public String getSelectedFilterValue() {
        return (String)valueDropdown.getSelectedItem();
    }
    
    public Comparator getSelectedComparator() {
        return (Comparator)comparatorDropdown.getSelectedItem();
    }
    
    public void setValidFields(EnumMap<FilterField, Set<String>> fields) {
        this.fields = fields;
        
        DefaultComboBoxModel<FilterField> cbm1 = new DefaultComboBoxModel<>();
        fields.keySet().stream().forEach((ff) -> {
            if(!ff.getCompareType().equals(FilterField.CompareType.DO_NOT_USE)) {
                cbm1.addElement(ff);
            }
        });
        fieldDropdown.setModel(cbm1);
    }
    
    public void clear() {
        fieldDropdown.setSelectedItem(FilterField.NONE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fieldDropdown = new javax.swing.JComboBox();
        comparatorDropdown = new javax.swing.JComboBox();
        valueDropdown = new javax.swing.JComboBox();

        fieldDropdown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fieldDropdownActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(fieldDropdown, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comparatorDropdown, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(valueDropdown, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldDropdown, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comparatorDropdown, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(valueDropdown, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void fieldDropdownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fieldDropdownActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fieldDropdownActionPerformed

    public enum Comparator {
        EQUALS("="),
        LESS_THAN("<"),
        GREATER_THAN(">"),
        IS("IS"),
        IS_NOT("IS NOT");
        
        private final String displayString;
        
        private Comparator(String displayString) {
            this.displayString = displayString;
        }
        
        @Override
        public String toString() {
            return displayString;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comparatorDropdown;
    private javax.swing.JComboBox fieldDropdown;
    private javax.swing.JComboBox valueDropdown;
    // End of variables declaration//GEN-END:variables
}
